/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import WebFeeds 1.0
import GoogleReader 1.0
import "../../../../shared/qml" as Shared
import "../../../../shared/qml/UIConstants.js" as UIConstants

Shared.CommonSheet {
    id: root
    property variant service
    property bool authenticationDataValid: false
    acceptButtonText: qsTr("Sign in")
    rejectButtonText: qsTr("Cancel")
    title: root.service ? root.service.serviceName() : "";
    busyText: qsTr("Checking")
    iconSource: root.service ? root.service.serviceImageUrl() : "";

    Connections {
        target: root.service
        onAccountVerificationComplete: root.onAccountVerificationComplete(result)
    }

    function updateAcceptButtonStatus() {
        if(root.service) {
            root.acceptButton.enabled
                    = ((userNameField.text.length > 0) && (passwordField.text.length > 0));
        }
    }

    function onAccountVerificationComplete(result) {
        if(result == GoogleReaderEnum.ErrNoError) {
            root.authenticationDataValid = true;
            root.close();
            return;
        } else if(result == GoogleReaderEnum.ErrAborted) {
            root.close();
            return;
        }
        root.busy = false;
        if(result == GoogleReaderEnum.ErrInvalidAccount) {
            root.showMessage(qsTr("Wrong username or password"));
        }
        else if(result == GoogleReaderEnum.ErrAborted) {
            root.close();
        }
        else if(result == GoogleReaderEnum.ErrNetworkError) {
            root.showMessage(qsTr("Network error"));
        }
    }

    onAccepted: {
        root.busy = true;
        root.open();
        root.service.verifyAuthenticationData(userNameField.text, passwordField.text);
    }

    onRejected: {
        if(root.busy)
        {
            root.open();
            root.service.abortVerification();
        }
    }

    onStatusChanged: {
        if((root.status == DialogStatus.Closed)
                && root.authenticationDataValid)
            root.service.setAuthenticationData(userNameField.text, passwordField.text);
    }

    contentItem: Column {
        id: dataEntryItem
        anchors.fill: parent
        anchors.leftMargin: UIConstants.DEFAULT_MARGIN
        anchors.rightMargin: UIConstants.DEFAULT_MARGIN
        spacing: UIConstants.DEFAULT_HALF_MARGIN

        Column {
            width: parent.width
            Label {
                text: qsTr("Username")
                font.family: UIConstants.FONT_FAMILY_LIGHT
            }
            Shared.TextField {
                id: userNameField
                width: parent.width
                inputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhPreferLowercase | Qt.ImhEmailCharactersOnly
                onTextChanged: {
                    root.updateAcceptButtonStatus();
                }
                platformSipAttributes: SipAttributes {
                    actionKeyEnabled: root.acceptButton.enabled
                    actionKeyLabel: root.acceptButtonText
                    actionKeyHighlighted: true
                }
                Keys.onReturnPressed: {
                    if(root.acceptButton.enabled)
                        root.accept();
                }
            }
        }
        Column {
            width: parent.width
            Label {
                text: qsTr("Password")
                font.family: UIConstants.FONT_FAMILY_LIGHT
            }
            Shared.TextField {
                id: passwordField
                width: parent.width
                echoMode: TextInput.Password
                inputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhPreferLowercase
                onTextChanged: {
                    root.updateAcceptButtonStatus();
                }
                platformSipAttributes: SipAttributes {
                    actionKeyEnabled: root.acceptButton.enabled
                    actionKeyLabel: root.acceptButtonText
                    actionKeyHighlighted: true
                }
                Keys.onReturnPressed: {
                    if(root.acceptButton.enabled)
                        root.accept();
                }
            }
        }
        Column {
            width: parent.width
            spacing: UIConstants.DEFAULT_MARGIN
            Label {
                anchors.horizontalCenter: parent.horizontalCenter
                text: qsTr("Don't have an account yet?")
            }
            Label {
                anchors.horizontalCenter: parent.horizontalCenter
                text: '<a href="https://accounts.google.com/NewAccount?service=reader">Get new account here</a>'
                font.bold: true
                onLinkActivated: Qt.openUrlExternally(link)
            }
        }
        Connections {
            target: root
            onStatusChanged: {
                if(root.status == DialogStatus.Open) {
                    userNameField.forceActiveFocus();
                    userNameField.platformOpenSoftwareInputPanel();
                }
            }
        }
    }
}
